// Numbers line from a text file.
// By DreamVB

#include <iostream>
#include <stdio.h>

using namespace std;
using std::cout;
using std::endl;

int main(int argc, char* argv[])
{ 
	FILE *fp = NULL;
	char line[255];
	char sLen[10];
	int i = 1;
	int len = 0;

	//Check for filename.
	if(argc != 2){
		cout << "Usage: " << argv[0] << " <Filename>";
		exit(1);
	}

	//Open file and counter number of lines.
	fp = fopen(argv[1],"r");

	//Check if file was opened.
	if(!fp){
		cout << "IO/Error, Faild to open file:" << endl << argv[1] << endl;
		exit(1);
	}

	while(fgets(line,255,fp) != NULL){
		//Keep line count
		i++;
	}
	//Rewind back to start of file
	rewind(fp);
	//Convert the number of lines to ascii
	itoa(i,sLen,10);
	//Get the length of the ascii string above
	len = strlen(sLen);
	//Retset i we use this again.
	i = 1;
	//While not end of file read in lines
	while(fgets(line,255,fp) != NULL){
		//Fill the left side of the line with line count
		cout.fill('0');
		//Set length of padding
		cout.width(len);
		//Output line index
		cout << i << " ";
		//Output line
		cout << line;
		//Keep line index going till no more lines
		i++;
	}

	//Close file
	fclose(fp);
	//Clear char arrays.
	memset(line,0,sizeof(line));
	memset(sLen,0,sizeof(sLen));
	//Ret
    return 0; 
} 
